﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Configuration;

namespace Admin.CYT369.Com.Utility
{
    /// <summary>
    /// 系统配置服务
    /// </summary>
    public class SystemSettingService
    {
        #region init
        private static readonly SystemSettingService m_Instance = new SystemSettingService();
        private SystemSettingService()
        {

#if DEBUG
            string basePath = System.IO.Directory.GetCurrentDirectory();
            if (!System.IO.File.Exists(System.IO.Path.Combine(basePath, "appsettings.json"))) { 
                basePath = new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).DirectoryName;
            }
#else
            string basePath = new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).DirectoryName;
#endif
            var builder = new ConfigurationBuilder()
                .SetBasePath(basePath).AddJsonFile(@"appsettings.json", optional: true, reloadOnChange: true);

            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; set; }

        /// <summary>
        /// 类型实例
        /// </summary>
        public static SystemSettingService Instance {
            get {
                return m_Instance;
            }
        }
        #endregion

        #region Connection
        private Connection m_admin_cyt369_com_connection_string;
        public Connection admin_cyt369_com_connection_string {
            get {
                if (m_admin_cyt369_com_connection_string == null)
                {
                    m_admin_cyt369_com_connection_string = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:admin_cyt369_com:DBType").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:admin_cyt369_com:ConnectionString").Value
                    };
                }

                return m_admin_cyt369_com_connection_string;
            }
        }

        private Connection m_user_admin_cyt369_com_connection_string;
        public Connection user_admin_cyt369_com_connection_string {
            get {
                if (m_user_admin_cyt369_com_connection_string == null)
                {
                    m_user_admin_cyt369_com_connection_string = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:user_admin_cyt369_com:DBType").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:user_admin_cyt369_com:ConnectionString").Value
                    };
                }

                return m_user_admin_cyt369_com_connection_string;
            }
        }


        private Connection m_news_admin_cyt369_com_connection_string;
        public Connection news_admin_cyt369_com_connection_string {
            get {
                if (m_news_admin_cyt369_com_connection_string == null)
                {
                    m_news_admin_cyt369_com_connection_string = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:news_admin_cyt369_com:DBType").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:news_admin_cyt369_com:ConnectionString").Value
                    };
                }

                return m_news_admin_cyt369_com_connection_string;
            }
        }

        private Connection m_brand_admin_cyt369_com_connection_string;
        public Connection brand_admin_cyt369_com_connection_string
        {
            get
            {
                if (m_brand_admin_cyt369_com_connection_string == null)
                {
                    m_brand_admin_cyt369_com_connection_string = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:brand_admin_cyt369_com:DBType").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:brand_admin_cyt369_com:ConnectionString").Value
                    };
                }

                return m_brand_admin_cyt369_com_connection_string;
            }
        }

        private Connection m_cheap_admin_cyt369_com_connection_string;
        public Connection cheap_admin_cyt369_com_connection_string {
            get {
                if (m_cheap_admin_cyt369_com_connection_string == null)
                {
                    m_cheap_admin_cyt369_com_connection_string = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:cheap_admin_cyt369_com:DBType").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:cheap_admin_cyt369_com:ConnectionString").Value
                    };
                }

                return m_cheap_admin_cyt369_com_connection_string;
            }
        }

        private Connection m_base_admin_cyt369_com_connection_string;
        public Connection base_admin_cyt369_com_connection_string {
            get {
                if (m_base_admin_cyt369_com_connection_string == null)
                {
                    m_base_admin_cyt369_com_connection_string = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:base_admin_cyt369_com:DBType").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:base_admin_cyt369_com:ConnectionString").Value
                    };
                }

                return m_base_admin_cyt369_com_connection_string;
            }
        }

        private Connection m_BaseDB;
        public Connection BaseDB
        {
            get
            {
                if (m_BaseDB == null)
                {
                    m_BaseDB = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:BaseDB:DBType").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:BaseDB:ConnectionString").Value
                    };
                }

                return m_BaseDB;
            }
        }
        #endregion

        #region SystemSetting
        public string AppName {
            get {
                return Configuration.GetSection("SystemSetting:AppName").Value;
            }
        }

        public string SystemName {
            get {
                return Configuration.GetSection("SystemSetting:SystemName").Value;
            }
        }

        public int PageSize {
            get {
                int id = 0;
                int.TryParse(Configuration.GetSection("SystemSetting:PageSize").Value, out id);
                if (id <= 0)
                {
                    id = 20;
                }
                return id;
            }
        }

        public string PageList {
            get {
                return Configuration.GetSection("SystemSetting:PageList").Value;
            }
        }

        /// <summary>
        /// 登录超时时间
        /// </summary>
        public int IdleTimeout {
            get {

                int n = 0;
                int.TryParse(Configuration.GetSection("SystemSetting:IdleTimeout").Value, out n);
                if (n <= 0)
                {
                    n = 120;
                }
                return n;
            }
        }

        /// <summary>
        /// 是否启用打印组件
        /// </summary>
        public bool EnableLodop {
            get {

                bool enableLodop = false;
                bool.TryParse(Configuration.GetSection("SystemSetting:EnableLodop").Value, out enableLodop);
                return enableLodop;
            }
        }

        #endregion

        #region Aliyun.OSS
        private Admin.CYT369.Com.Utility.EndpointParameter m_NewsInternalBucket;

        /// <summary>
        /// 内容管理系统图片/文件库
        /// </summary>
        public Admin.CYT369.Com.Utility.EndpointParameter NewsInternalBucket
        {
            get
            {
                if (m_NewsInternalBucket == null)
                {
                    m_NewsInternalBucket = new Admin.CYT369.Com.Utility.EndpointParameter()
                    {
                        Endpoint = Configuration.GetSection("Aliyun.OSS:News:Internal").Value,
                        AccessKeyId = Configuration.GetSection("Aliyun.OSS:News:AccessKeyId").Value,
                        AccessKeySecret = Configuration.GetSection("Aliyun.OSS:News:AccessKeySecret").Value,
                        Bucket = Configuration.GetSection("Aliyun.OSS:News:Public.Bucket").Value,
                        HttpURL = Configuration.GetSection("Aliyun.OSS:News:Public.Bucket.HTTP").Value
                    };
                }

                return m_NewsInternalBucket;
            }
        }

        private Admin.CYT369.Com.Utility.EndpointParameter m_BrandInternalBucket;

        /// <summary>
        /// 内容管理系统图片/文件库
        /// </summary>
        public Admin.CYT369.Com.Utility.EndpointParameter BrandInternalBucket
        {
            get
            {
                if (m_BrandInternalBucket == null)
                {
                    m_BrandInternalBucket = new Admin.CYT369.Com.Utility.EndpointParameter()
                    {
                        Endpoint = Configuration.GetSection("Aliyun.OSS.Brand:Internal").Value,
                        AccessKeyId = Configuration.GetSection("Aliyun.OSS.Brand:AccessKeyId").Value,
                        AccessKeySecret = Configuration.GetSection("Aliyun.OSS.Brand:AccessKeySecret").Value,
                        Bucket = Configuration.GetSection("Aliyun.OSS.Brand:Public.Bucket").Value,
                        HttpURL = Configuration.GetSection("Aliyun.OSS.Brand:Public.Bucket.HTTP").Value
                    };
                }

                return m_BrandInternalBucket;
            }
        }
        #endregion

        #region path
        public string LoginPath {
            get {
                var path = Configuration.GetSection("AppPath:LoginPath").Value;
                return path;
            }
        }

        #endregion

        #region OSS Config
        private EndpointParameter m_NewsBucket;
        public EndpointParameter NewsBucket {
            get {
                if (m_NewsBucket == null)
                {
                    m_NewsBucket = new EndpointParameter()
                    {
                        Endpoint = Configuration.GetSection("Aliyun.OSS.News:Internal").Value,
                        AccessKeyId = Configuration.GetSection("Aliyun.OSS.News:AccessKeyId").Value,
                        AccessKeySecret = Configuration.GetSection("Aliyun.OSS.News:AccessKeySecret").Value,
                        Bucket = Configuration.GetSection("Aliyun.OSS.News:Public.Bucket").Value,
                        HttpURL = Configuration.GetSection("Aliyun.OSS.News:Public.Bucket.HTTP").Value
                    };
                }

                return m_NewsBucket;
            }
        }
        private EndpointParameter m_BrandBucket;
        public EndpointParameter BrandBucket
        {
            get
            {
                if (m_BrandBucket == null)
                {
                    m_BrandBucket = new EndpointParameter()
                    {
                        Endpoint = Configuration.GetSection("Aliyun.OSS.Brand:Internal").Value,
                        AccessKeyId = Configuration.GetSection("Aliyun.OSS.Brand:AccessKeyId").Value,
                        AccessKeySecret = Configuration.GetSection("Aliyun.OSS.Brand:AccessKeySecret").Value,
                        Bucket = Configuration.GetSection("Aliyun.OSS.Brand:Public.Bucket").Value,
                        HttpURL = Configuration.GetSection("Aliyun.OSS.Brand:Public.Bucket.HTTP").Value
                    };
                }

                return m_BrandBucket;
            }
        }

        private EndpointParameter m_CheapBucket;
        public EndpointParameter CheapBucket
        {
            get
            {
                if (m_CheapBucket == null)
                {
                    m_CheapBucket = new EndpointParameter()
                    {
                        Endpoint = Configuration.GetSection("Aliyun.OSS.Cheap:Internal").Value,
                        AccessKeyId = Configuration.GetSection("Aliyun.OSS.Cheap:AccessKeyId").Value,
                        AccessKeySecret = Configuration.GetSection("Aliyun.OSS.Cheap:AccessKeySecret").Value,
                        Bucket = Configuration.GetSection("Aliyun.OSS.Cheap:Public.Bucket").Value,
                        HttpURL = Configuration.GetSection("Aliyun.OSS.Cheap:Public.Bucket.HTTP").Value
                    };
                }

                return m_CheapBucket;
            }
        }

        private EndpointParameter m_BaseBucket;
        public EndpointParameter BaseBucket
        {
            get
            {
                if (m_BaseBucket == null)
                {
                    m_BaseBucket = new EndpointParameter()
                    {
                        Endpoint = Configuration.GetSection("Aliyun.OSS.Base:Internal").Value,
                        AccessKeyId = Configuration.GetSection("Aliyun.OSS.Base:AccessKeyId").Value,
                        AccessKeySecret = Configuration.GetSection("Aliyun.OSS.Base:AccessKeySecret").Value,
                        Bucket = Configuration.GetSection("Aliyun.OSS.Base:Public.Bucket").Value,
                        HttpURL = Configuration.GetSection("Aliyun.OSS.Base:Public.Bucket.HTTP").Value
                    };
                }

                return m_BaseBucket;
            }
        }
        #endregion


    }
}
